<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-button class="filter-item" type="primary" v-waves icon="el-icon-plus" @click="handleUpdate(null)">添加</el-button> -->
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="微信昵称" v-model="filters.like$nickname"></el-input> 
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="手机号" v-model="filters.telephone"></el-input> 
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-document" @click="exportExcelFile">导出</el-button>
    </div>
    <el-table :data="list" @sort-change="sortChange" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
      style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="80">
      </el-table-column>
      <el-table-column prop="avatar" align="center" label="微信头像" width="120">
        <template slot-scope="scope">
          <img :src="scope.row.avatar|absUrl" class="avatar" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="nickname" align="center" label="微信昵称" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.nickname}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="telephone" align="center" label="手机号" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.telephone}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="username" align="center" label="姓名" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.username}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="email" align="center" label="邮箱" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.email}}</span>
        </template>
      </el-table-column>
      <!--         
        <el-table-column  prop="wallet" align="center" label="余额" width="120">
          <template slot-scope="scope">
            <span>{{scope.row.wallet}}</span>
          </template>
        </el-table-column>

      <el-table-column prop="orderCount" align="center" label="订单数" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.orderCount}}</span>
        </template>
      </el-table-column>
 -->

      <el-table-column prop="virtualCard" align="center" label="会员卡号" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.virtualCard}}</span>
        </template>
      </el-table-column>


      <el-table-column prop="points" align="center" label="积分" width="120">
        <template slot-scope="scope">
          <span>{{scope.row.points}}</span>
        </template>
      </el-table-column>
      <!--         
        
        <el-table-column  prop="totalPoints" align="center" label="历史积分" width="120">
          <template slot-scope="scope">
            <span>{{scope.row.totalPoints}}</span>
          </template>
        </el-table-column> -->





      <el-table-column align="center" label="操作" width="200" class-name="small-padding">
        <template slot-scope="scope">
          <!-- <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">修改</el-button> -->
          <el-button size="mini" v-if="scope.row.state==1" :loading="scope.row.isChangingState" type="danger" @click="stopUser(scope.row)">禁用</el-button>
          <el-button size="mini" v-if="scope.row.state==0" :loading="scope.row.isChangingState" type="primary" @click="startUser(scope.row)">启动</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="params.page"
        :page-sizes="[10,20,30,50]" :page-size="params.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="isEditing?'修改':'添加'" :visible.sync="dialogFormVisible">
      <el-form class="dialog-form" :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="120px">

        <el-form-item label="用户名" prop="username">
          <el-input v-model="temp.username"  placeholder="用户名"></el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="telephone">
          <el-input v-model="temp.telephone" placeholder="手机号"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">


          <el-input v-model="temp.email" placeholder="邮箱"></el-input>


        </el-form-item>

        <el-form-item label="余额" prop="wallet">


          <el-input v-model="temp.wallet" placeholder="余额"></el-input>


        </el-form-item>

        <el-form-item label="订单数" prop="orderCount">


          <el-input v-model="temp.orderCount" placeholder="订单数"></el-input>


        </el-form-item>

        <el-form-item label="会员卡号" prop="carNo">


          <el-input v-model="temp.carNo" placeholder="会员卡号"></el-input>


        </el-form-item>

        <!-- <el-form-item label="积分" prop="points">


          <el-input v-model="temp.points" placeholder="积分"></el-input>


        </el-form-item> -->

        <!-- <el-form-item label="历史积分" prop="totalPoints">


          <el-input v-model="temp.totalPoints" placeholder="历史积分"></el-input>


        </el-form-item> -->

        <!-- <el-form-item label="openid" prop="openid">


          <el-input v-model="temp.openid" placeholder="openid"></el-input>


        </el-form-item> -->

        <el-form-item label="微信昵称" prop="nickname">


          <el-input v-model="temp.nickname" placeholder="微信昵称"></el-input>


        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="submiting" @click="submitFormAction">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      temp: {
        id: "",

        username: "",

        telephone: "",

        email: "",

        wallet: "",

        orderCount: "",

        carNo: "",

        points: "",



        nickname: ""
      },

      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空",
            trigger: "change",
            max: 60
          }
        ],

        telephone: [
          {
            required: true,
            message: "手机号不能为空",
            trigger: "change",
            max: 11
          }
        ],

        email: [
          {
            required: true,
            message: "邮箱不能为空",
            trigger: "change",
            max: 60
          }
        ],

        wallet: [
          {
            required: true,
            message: "余额不能为空",
            trigger: "change"
          }
        ],

        orderCount: [
          {
            required: true,
            message: "订单数不能为空",
            trigger: "change"
          }
        ],

        carNo: [
          {
            required: true,
            message: "会员卡号不能为空",
            trigger: "change",
            max: 30
          }
        ],

        points: [
          {
            required: true,
            message: "积分不能为空",
            trigger: "change"
          }
        ],

        totalPoints: [
          {
            required: true,
            message: "历史积分不能为空",
            trigger: "change"
          }
        ],

        openid: [
          {
            required: true,
            message: "openid不能为空",
            trigger: "change",
            max: 30
          }
        ],

        nickname: [
          {
            required: true,
            message: "微信昵称不能为空",
            trigger: "change",
            max: 60
          }
        ]
      }
    };
  },
  created() {
    this.init("user", 1).loadData();
  },
  filters: {},
  methods: {
    stopUser(row){
      this.$confirm('确定禁用该用户吗？','提示').then(_=>{
          this.$set(row,'isChangingState',true);
          this.$post('/admin/user/stop_user',{
            id:row.id
          }).then(res=>{
             this.$set(row,'isChangingState',false);
             row.state = 0;
             this.$$notify();
          }).catch(_=>{
             this.$set(row,'isChangingState',false);
          });
      }).catch(_=>{
      });
    },
    startUser(row){
       this.$confirm('确定启用该用户吗？','提示').then(_=>{
          this.$set(row,'isChangingState',true);
          this.$post('/admin/user/start_user',{
            id:row.id
          }).then(res=>{
             this.$set(row,'isChangingState',false);
             row.state = 1;
             this.$$notify();
          }).catch(_=>{
             this.$set(row,'isChangingState',false);
          });
      }).catch(_=>{
      });
    },
    exportExcelFile() {
      this.exportExcel({
        title: "会员列表",
        fields: [
           

          {
            field: "nickname",
            title: "微信昵称"
          },
          {
            field: "username",
            title: "用户名"
          },

          {
            field: "telephone",
            title: "手机号"
          },

          {
            field: "email",
            title: "邮箱"
          },

          {
            field: "carNo",
            title: "会员卡号"
          },

          {
            field: "points",
            title: "积分"
          }
          
        ]
      }).then(info => {
        this.openWin(info);
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.avatar{
  width:50px;
  height:50px;
  display: block;
  margin:0 auto;
  border-radius: 50%;
}
</style>
